import {defineStore} from 'pinia'
import {getCarousel} from "@/api/index/carousel";
import {formatUrl} from "@/utils/tools/format";
import defUrl from  "@/assets/img/banner/rand.jpg"
export const useCarouselStore = defineStore("carousel",{
    persist: true, //开启持久化
    state: () => ({
        carousel:[{
            carouselUrl:defUrl
        }],
        loading:true,

    }),
    getters:{
        carouselUrl:(state)=>{
            state.carousel.forEach(item =>{
                item.carouselUrl = formatUrl(item.carouselUrl)
            })
           return state.carousel
        },
        isLoading:(state)=>{
            return state.loading
        },
    },
    actions: {
        /**
         * 获取轮播图
         */
        async getCarouselList(){
            getCarousel().then(res=>{
                if (res.data.length > 0){
                    this.carousel = res.data;
                }
            })
        },
        setLoading(loading){
            this.loading = loading
        }
    },
})
